<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/page/public/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>部门管理</title>
<%@ include file="/WEB-INF/page/public/contentmeta.jsp"%>
<link rel="stylesheet" href="/plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/global.css" media="all" />
<link rel="stylesheet" href="/css/tab.css" media="all" />
</head>
<body>

<div class="admin-tab admin-user">
    <div class="tab-condition">
        <form class="clefix"  action="/user/getuserlist" method="post"> <!-- layui-form -->
            <%-- <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="text" name="keyWord" value="${pager.params.get('keyWord')}"  lay-verify="title" autocomplete="off" placeholder="请输入名字" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item department">
				<div class="layui-input-inline">
					<select   name="deptid">
						<option value="">所在部门</option>
						<dic:dp value="${pager.params.get('deptid')}" type="2"/>
					</select>
				</div>
			</div> --%>
            <div class="layui-form-item">
            	<input name="skipToPage" id="skipToPage"  type="hidden" value="${pager.pageNo}">
				<input type="hidden" id="pageNo" name="pageNo" value="${pager.pageNo}">
				<input type="hidden" id="totalPage" name="totalPage" value="${pager.totalPages}">
               <!--  <button class="layui-btn" lay-submit="" lay-filter="demo2"><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>搜索</button> -->
            </div>
        </form>
        <div class="TM-tab-info">
        	<a class="layui-btn layui-btn-primary layui-btn-small adddept">+添加部门</a>
            <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary layui-btn-small">
        		<i class="layui-icon">&#xe603;</i>返回
        	</a>
        </div>
    </div>
    <div class="layui-field-box">
        <table class="site-table table-hover TM-user">
            <thead>
            <tr>
                <th style="width:20%;">部门名称</th>
                <th style="width:10%;">部门人数</th>
                <th style="width:15%;">部门等级</th>
                <th style="width:15%;">备注</th>
                <th style="width:20;">操作</th>
            </tr>
            </thead>
            <tbody>
            	<c:forEach var="deptlist" items="${pager.resultList}">
            	 <tr>
	                <td><a href="/systerm/getdepartmentlist${deptlist.id}">${deptlist.name}</a></td>
	                <td>${deptlist.count}</td>
	                <td>${deptlist.level}</td>
	                <td>${deptlist.remark}</td>
	                <td>
	                	<c:if test="${deptlist.parentid != 0}">
	                		<button class="layui-btn layui-btn-primary layui-btn-small update" namedata="${deptlist.name}" iddata="${deptlist.id}" piddata="${deptlist.parentid}"><i class="layui-icon ">&#xe642;</i>修改</button>
	                		<button class="layui-btn layui-btn-primary layui-btn-small delete" iddata="${deptlist.id}"><i class="layui-icon ">&#xe640;</i>删除</button>
	                	</c:if>
	                </td>
	            </tr>
			</c:forEach>
            </tbody>
        </table>
        <div class="admin-table-page">
            <div id="page" class="page"></div>
        </div>	
    </div>
</div>

<div class="layui-form-item" id="validatebound-formDiv" style="width:90%;margin-top:20px;display:none">
	<form  class="layui-form clefix" action="" id="validatebound-form" method="post">
		<div class="layui-form-item">
		    <label class="layui-form-label">部门名称</label>
		    <div class="layui-input-block">
		    	<input type="text" class="layui-input" id="deptname">
		    </div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">父部门</label>
		    <div class="layui-input-block">
		    	<select id="deptparentname" name="deptparentname" lay-filter="rolename">
					  <dic:dp type="3" value=""/>
				</select>
		    </div>
		</div>
		<div class="layui-form-item">
    		<div class="layui-input-block">
    			<input type="hidden" id="type" name="type">
    			<input type="hidden" id="id" name="id">
     			<button class="layui-btn" lay-submit  lay-filter="departmentsubmit">保存</button>
	    	</div>
		</div>  
	</form>
</div>


<script>

	layui.use(['icheck','element','laypage','form'], function() {
		
		var $ = layui.jquery,
        laypage = layui.laypage,
        element = layui.element(); //Tab的切换功能，切换事件监听等，需要依赖element模块
 		var form = layui.form();
        
 		//添加部门
 		$(".adddept").on("click",function(){
 			$("#type").val(0);
 			layer.open({
				title: '添加部门',
				maxmin: true,
				type: 1,
				content:  $('#validatebound-formDiv'),
				area: ['500px', '400px']
			});
 		});
 		
 		
 		//删除部门
 		$(".delete").on("click",function(){
 			var id = $(this).attr("iddata");
 			layer.confirm('确定删除该部门吗？', {
 				  btn: ['确定','取消'] 
 			}, function(){
 				layer.confirm('该部门删除后，其所有子部门都将被删除，确定删除？',{
 					btn: ['确定','取消']
 				},function(){
 					$.ajax({
 						type : "post",
 						data : {id : id},
 						url : "/systerm/deletedepartment",
 						dataType : "json",
						success : function(data){
							layer.msg("删除成功",{icon: 1,time:500},function(){ window.location.reload();});
						} 						
 					});
 				},function(){
 					layer.msg("取消删除",{icon: 1,time:700});
 				});
 			}, function(){
 				layer.msg("取消删除",{icon: 1,time:700});
 			});
 		});
 		
 		
 		//修改部门 		
 		$(".update").on("click",function(){
 			var id = $(this).attr("iddata");
 			var pid = $(this).attr("piddata");
 			var name = $(this).attr("namedata");
 			$("#type").val(1);
 			$("#deptparentname").val(pid);
 			$("#deptname").val(name);
 			$("#id").val(id);
 			form.render(); 
 			layer.open({
				title: '修改部门',
				maxmin: true,
				type: 1,
				content:  $('#validatebound-formDiv'),
				area: ['500px', '400px']
			});
 			
 		});
 		
        
    	var pageNo = $("#pageNo").val();
    	var totalPage = $("#totalPage").val(); 
        //page
        laypage({
            cont: 'page',
            curr: pageNo//'${pager.pageNo}'
            ,pages: totalPage//'${pager.totalPages}' //总页数
            ,
            groups: 5 //连续显示分页数
            ,
            jump: function(obj, first) {
                //得到了当前页，用于向服务端请求对应数据
                var curr = obj.curr;
                if(!first) {
                	$("#skipToPage").val(curr);
                	$("#userlistform").submit();
                }
            }
        });
        
        
	});
	
	$(function(){
		$("#validatebound-form").validate({
			rules: { },
			messages: { },
			submitHandler: function(form) {
				var name = $("#deptname").val();
				var parentid = $("#deptparentname").val();
				var type = $("#type").val();
				var id = $("#id").val();
				$.ajax({
					type : "post",
					url : "/systerm/adddepartment",
					data : {name : name , parentid : parentid , type : type , id : id},
					dataType : "json",
					success : function(data){
						layer.msg("添加成功!", {icon: 1,time:500},function(){ window.location.href="/systerm/getdepartmentlist";});
					}					
				});
	  		}
		});
	});
	
    //获取被选中的checkbox
    function getCheckedId(){
   	   var ids = "";
   	  $("[name='checkbox']").each(function(){
   		   if($(this).is(":checked")){
   			     ids +=$(this).val()  +"," 
   		   } 
   	  })
   	 return ids
   }


</script>

</body>
</html>